package com.asr.testclient.view;

import com.asr.testclient.controller.MainController;

import javax.swing.*;
import java.awt.*;

/**
 * 主窗口
 * 组织所有GUI面板
 */
public class MainFrame extends JFrame {
    private ConnectionPanel connectionPanel;
    private InputPanel inputPanel;
    private ResultPanel resultPanel;

    public MainFrame() {
        initComponents();
        layoutComponents();
        configureWindow();
    }

    private void initComponents() {
        connectionPanel = new ConnectionPanel();
        inputPanel = new InputPanel();
        resultPanel = new ResultPanel();
    }

    private void layoutComponents() {
        setLayout(new BorderLayout(10, 10));

        // 添加边距
        JPanel contentPanel = new JPanel(new BorderLayout(10, 10));
        contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 顶部: 连接面板
        contentPanel.add(connectionPanel, BorderLayout.NORTH);

        // 中部: 输入面板
        contentPanel.add(inputPanel, BorderLayout.CENTER);

        // 底部: 结果面板
        contentPanel.add(resultPanel, BorderLayout.SOUTH);

        add(contentPanel);
    }

    private void configureWindow() {
        setTitle("ASR服务器测试客户端");
        setMinimumSize(new Dimension(800, 600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示
    }

    /**
     * 设置控制器，将控制器传递给各面板
     */
    public void setController(MainController controller) {
        // 连接面板按钮事件
        connectionPanel.getConnectButton().addActionListener(e -> controller.handleConnect());
        connectionPanel.getDisconnectButton().addActionListener(e -> controller.handleDisconnect());

        // 输入面板按钮事件
        inputPanel.getSelectFileButton().addActionListener(e -> controller.handleSelectFile());
        inputPanel.getSubmitButton().addActionListener(e -> controller.handleSubmit());
        inputPanel.getClearInputButton().addActionListener(e -> controller.handleClearInput());

        // 结果面板按钮事件
        resultPanel.getClearResultButton().addActionListener(e -> controller.handleClearResult());
    }

    /**
     * 获取连接面板
     */
    public ConnectionPanel getConnectionPanel() {
        return connectionPanel;
    }

    /**
     * 获取输入面板
     */
    public InputPanel getInputPanel() {
        return inputPanel;
    }

    /**
     * 获取结果面板
     */
    public ResultPanel getResultPanel() {
        return resultPanel;
    }
}
